package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.ro.manager.DictionaryRo;
import com.credithc.signature.common.service.DictionaryService;
import com.credithc.signature.common.vo.manager.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描 述: 字典数据Controller<br/>
 * 作 者: JingBo Hu<br/>
 * 创 建：2019/5/24 16:55<br/>
 * 版 本：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
public class DictionaryController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    DictionaryService dictionaryService;


    /**
     * 查询所有
     *
     * @param dictionaryRo
     * @return
     */
    @PostMapping("/dictionary/list")
    public Message queryAll(@RequestBody DictionaryRo dictionaryRo) {
        PageInfo<DictionaryVo> pageInfo = dictionaryService.queryAll(dictionaryRo);
        Map map = new HashMap<>(16);
        map.put("pageInfo", pageInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("/dictionary/one/{id}")
    public Message queryOne(@PathVariable Long id) {
        DictionaryVo vo = dictionaryService.queryOne(id);
        Map map = new HashMap<>(16);
        map.put("vo", vo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询字典类型
     *
     * @return
     */
    @PostMapping("/dictionary/dictType/list")
    public Message queryDictType() {
        List<DictionaryVo> vos = dictionaryService.queryDictTypes();
        Map map = new HashMap<>(16);
        map.put("list", vos);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 新增
     *
     * @param dictionaryRo
     * @return
     */
    @PostMapping("/dictionary/save")
    public Message save(@RequestBody DictionaryRo dictionaryRo) {
        dictionaryService.save(dictionaryRo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 修改
     *
     * @param dictionaryRo
     * @return
     */
    @PostMapping("/dictionary/update")
    public Message update(@RequestBody DictionaryRo dictionaryRo) {
        dictionaryService.update(dictionaryRo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/dictionary/remove/{id}")
    public Message remove(@PathVariable Long id) {
        dictionaryService.remove(id);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }
}
